#include <time.h>
#include <math.h>
#include "My_CortexM3_Def.h"

static void delaycnt();

static void delaycnt()
{
  // for(;n>0;n--);
  int i;
  long double d;
  
  for(i=0;i<1000;i++) {
    d = sin((double)i)  * cos((double)i); 
  }
}

int main(void)
{
    clock_t start,secs;
    int     i,j,k=0;
    double  d;

    // GPIOB enable
    RCC_APB2ENR |= RCC_APB2PERIPH_GPIOB; 
  
    // clear port5 and set 1 = ON
    GPIOB_CRL &= ~(0xf<<(4*5));
    GPIOB_CRL |= 1<<(4*5);
  
    // clear port8 and set 1 = ON 
    GPIOB_CRH &= ~(0xf);
    GPIOB_CRH |= 1;

    // clear port9 and set 1 = ON 
    GPIOB_CRH &= ~(0xf<<(4*1));
    GPIOB_CRH |= 1<<4;  

    GPIOB_BSRR |= GPIO_LED1_PIN; // LED1 OFF
    GPIOB_BSRR |= GPIO_LED2_PIN; // LED2 OFF
    GPIOB_BSRR |= GPIO_LED3_PIN; // LED3 OFF
    
    for(j=0;j<5;j++) {
      GPIOB_BRR  |= GPIO_LED3_PIN;  
      delaycnt();
      GPIOB_BSRR |= GPIO_LED3_PIN;
      delaycnt(); 
   }
   
   //------------------------------------
   
    //start = clock();
    for(i=1;i<=10000;i++) {
      if((i%500)==0) {
        k++;
        if( k%2 )                 GPIOB_BRR  |= GPIO_LED2_PIN;
        else                      GPIOB_BSRR |= GPIO_LED2_PIN;
      }
       d = sin((double)i) * cos((double)i);
    }
  
    //secs = (clock() - start) / CLOCKS_PER_SEC;
    //i = (int)secs;    
    
   GPIOB_BSRR |= GPIO_LED1_PIN;
   GPIOB_BSRR |= GPIO_LED2_PIN;
   GPIOB_BSRR |= GPIO_LED3_PIN;
   delaycnt();delaycnt();delaycnt(); 
    
    for(j=0;j<3;j++) {
      GPIOB_BRR  |= GPIO_LED1_PIN;  
      delaycnt();
      GPIOB_BSRR |= GPIO_LED1_PIN;
      delaycnt();
   }
   GPIOB_BRR  |= GPIO_LED1_PIN;  
}